package com.yilin.tms.system.controller;

import com.google.gson.reflect.TypeToken;
import com.yilin.commons.util.JsonUtil;
import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.entity.system.system.Menu;
import com.yilin.tms.core.entity.system.system.MenuOperate;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.system.service.IMenuService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理
 */
@RestController
@RequestMapping("/system/menu/")
public class MenuController extends BaseController {

    @Resource
    IMenuService menuService;

    /**
     * 获取节点List
     */
    @PostMapping("getAllMenuList")
    public ReturnData<List<Menu>> getAllMenuList(Menu query) {
        List<Menu> menuList = menuService.getMenuList(query);
        return jsonView(true, "获取列表成功", menuList);
    }

    /**
     * 根据母菜单获取子菜单
     */
    @PostMapping("getChildMenuList")
    public ReturnData<List<Menu>> getChildMenuList(Menu query,String parentId) {
        if (parentId == null) parentId = "0";
        List<Menu> menuList = menuService.getChildMenuList(query,parentId);
        return jsonView(true, "获取列表成功", menuList);
    }


    /**
     * 通过IDS集合获取菜单集合
     * */
    @PostMapping("getMenuListByCodes")
    public ReturnData<List<Menu>> getMenuListByCodes( String[] menuCodes) {
        if (menuCodes == null) return jsonView(false, "请提供要查询的节点ID集");
        List<Menu> menuList = menuService.getMenuListByCodes(menuCodes);
        menuList.sort(Comparator.comparingInt(Menu::getSoft));
        return jsonView(true, "获取成功",menuList);
    }

    /**
     * 通过IDS集合获取菜单集合
     * 角色：获取只属于自己的菜单
     * */
    @PostMapping("getMenuListByUserTypeAndCodes")
    public ReturnData<List<Menu>> getMenuListByUserTypeAndCodes(UserType userType, String[] menuCodes) {
        if (menuCodes == null) return jsonView(false, "请提供要查询的节点ID集");
        List<Menu> menuList = menuService.getMenuListByUserTypeAndCodes(userType,menuCodes);
        menuList.sort(Comparator.comparingInt(Menu::getSoft));
        return jsonView(true, "获取成功",menuList);
    }

    /**
     * 添加菜单节点
     */
    @PostMapping("saveMenu")
    public ReturnData<Object> saveMenu(Menu menu) {
        if (menu.getMenuLevel() == null) {
            return jsonView(false, "未指定节点层级1");
        }
        if (StringUtil.isEmpty(menu.getTitle())){
            return jsonView(false, "请填写节点标题名称2");
        }
        if (StringUtil.isEmpty(menu.getName())){
            return jsonView(false, "请填写节点路由名称");
        }
        if (StringUtil.isEmpty(menu.getPath())){
            return jsonView(false, "请填写节点路由地址");
        }
        if (menu.getMenuType() == null) {
            return jsonView(false, "未指定节点类型");
        }
        if (menu.getMenuLevel()!=0 && menu.getParentId()==null){
            return jsonView(false, "请指定节点上级");
        }
        if (menu.getMenuType() != Menu.MenuType.layout && StringUtil.isEmpty(menu.getTargetUserTypes())){
            return jsonView(false, "请至少标记一个归类用户类型");
        }
        if (menu.getMenuType() == Menu.MenuType.source){
            if (!menu.getPath().contains("/")) menu.setCode(menu.getPath());
        }
        if (menu.getMenuType()== Menu.MenuType.layout) {
            menu.setComponent("Layout");
        }
        if (StringUtil.isEmpty(menu.getComponent())) {
            return jsonView(false, "请指定指定菜单VUE地址");
        }

        menuService.saveMenu(menu);
        return jsonView(true, "保存成功");
    }

    /**
     * 删除菜单节点
     */
    @PostMapping("removeMenu")
    public ReturnData<Object> removeMenu(Menu menu) {
        if (menu.getId() == null) return jsonView(false, "未指定节点");
        List<Menu> childList = menuService.getMenuByParentId(menu.getId());
        if (childList.size() > 0){
            return jsonView(false, "节点下存在子节点，请先删除子节点");
        }
        menuService.removeById(menu);
        menuService.deleteMenuOperateByMenuId(menu.getId());
        return jsonView(true, "删除成功");
    }

    /**
     * 查询操作
     * */
    @PostMapping("getMenuOperateListByMenuId")
    public ReturnData<Object> getMenuOperateListByMenuId(String menuId) {
        List<MenuOperate> menuOperateList = menuService.getMenuOperateListByMenuId(menuId);
        return jsonView(true, "获取列表成功", menuOperateList);
    }

    /**
     * 查询操作 不推荐
     * */
    @PostMapping("getMenuOperateListByMenuCode")
    public ReturnData<Object> getMenuOperateListByMenuCode(String menuCode) {
        List<MenuOperate> menuOperateList = menuService.getMenuOperateListByMenuCode(menuCode);
        return jsonView(true, "获取列表成功", menuOperateList);
    }

    /**
     * 查询操作 推荐
     * */
    @PostMapping("getMenuOperateListByMenuCodes")
    public ReturnData<Object> getMenuOperateListByMenuCodes(String[] menuCodes) {
        Map<String,List<MenuOperate>> operateMap = new HashMap<>();
        for (String menuCode : menuCodes) {
            List<MenuOperate> menuOperateList = menuService.getMenuOperateListByMenuCode(menuCode);
            operateMap.put(menuCode,menuOperateList);
        }
        return jsonView(true, "获取列表成功", operateMap);
    }

    /**
     * 查询操作 不推荐
     * */
    @PostMapping("getMenuOperateListByCodes")
    public ReturnData<Object> getMenuOperateListByCodes(String menuCode, String[] menuOperateCodes) {
        if (menuOperateCodes == null) return jsonView(false, "请提供要查询的节点Code集");
        List<MenuOperate> menuOperateList = menuService.getMenuOperateListByCodes(menuCode,menuOperateCodes);
        return jsonView(true, "获取列表成功", menuOperateList);
    }

    /**
     * 查询操作 推荐
     * */
    @PostMapping("getMenuOperateListMapByCodes")
    public ReturnData<Object> getMenuOperateListMapByCodes(String menuCodeOperateCodesJson) {
        if (menuCodeOperateCodesJson == null) return jsonView(false, "请提供要查询的节点Code集");
        Map<String,String[]> menuCodeOperateCodes = JsonUtil.jsonToObject(menuCodeOperateCodesJson,new TypeToken<Map<String,String[]>>(){});
        Map<String,List<MenuOperate>> operateMap = new HashMap<>();
        for (String menuCode : menuCodeOperateCodes.keySet()) {
            String [] menuOperateCodes = menuCodeOperateCodes.get(menuCode);
            if (menuOperateCodes.length==0) continue;
            List<MenuOperate> menuOperateList = menuService.getMenuOperateListByCodes(menuCode,menuOperateCodes);
            operateMap.put(menuCode,menuOperateList);
        }
        return jsonView(true, "获取列表成功", operateMap);
    }

    /**
     * 添加操作
     * */
    @PostMapping("saveMenuOperate")
    public ReturnData<Object> saveMenuOperate(MenuOperate operate) {
        if (operate.getMenuId() == null) return jsonView(false, "未指定菜单节点");
        Menu menu = menuService.getById(operate.getMenuId());
        if (operate.getId() == null){
            MenuOperate local = menuService.getMenuOperateByCode(operate.getMenuId(),operate.getCode());
            if (local!=null) return jsonErrView("添加失败：操作码已存在");
        }
        operate.setMenuName(menu.getTitle());
        operate.setMenuCode(menu.getCode());
        menuService.saveMenuOperate(operate);
        return jsonView(true, "保存成功");
    }

    /**
     * 删除操作
     */
    @PostMapping("removeMenuOperate")
    public ReturnData<Object> removeMenuOperate(MenuOperate operate) {
        if (operate.getId() == null) return jsonView(false, "未指定数据");
        menuService.deleteMenuOperateById(operate.getId());
        return jsonView(true, "删除成功");
    }
}
